<?php

/*
 * This class is used for common same function.
 */

class Model_Quotesfile extends Model_Base {
	
	protected $_name,$_primaryKey;
    
    public function init() {
        $this->_name = $this->QUOTES_FILE_TBL;
        $this->_primaryKey = 'qf_id';
    }
	
	 
	public function getStatusArray() {
        return array(
            "" => "-- Select Status --",
            "1" => "Active",
            "0" => "Inactive"
        );
    }
	
	
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('qf' => $this->QUOTES_FILE_TBL))
                ->where('qf_id = ?', $id)
				->where("qf_status != 2");
     
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
	
	public function fetchQuotefile($where = "", $limit = "", $offset = "0", $order = 'qf_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('qf' => $this->QUOTES_FILE_TBL));
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);
        #echo $select;exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
	
	public function fetchQuoteFileByQuoteId($p_id = "") {

        $finalArray = Array();
        $finalArray['DOCUMENT'] = Array();

        if ($p_id == "") {
            return $finalArray;
        }
        $quoteImages = $this->fetchQuotefile("qf_id = '" . $p_id . "'");
        if (count($quoteImages) > 0) {
            foreach ($quoteImages as $_img) {
                $finalArray['DOCUMENT'][] = $_img;
            }
        }
        return $finalArray;
    }
	
	public function fetchENTRYBYImage($img_name) {
        $select = $this->select()
                ->from(array('qf' => $this->QUOTES_FILE_TBL))
                 ->setIntegrityCheck(false)
                ->join(array('quote' => $this->QUOTES_TBL), "qf.qf_quote_id = quote.quote_id",array('qf.qf_id'))
                ->where('qf_filename = "'.$img_name.'"')
                ->where("qf_status != 2");

        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
	
	 public function fetchQuotefileData($where = "", $limit = "", $offset = "0", $order = "qf_id", $sort = "ASC") {
       $select = $this->select()
                ->from(array("qf" => $this->QUOTES_FILE_TBL))
				//->setIntegrityCheck(false)
				//->join(array('lgn' => $this->LOGIN_TBL), "qf.quote_responsible = lgn.lgn_id")
				->where("qf_status != 2");
        if ($where != "") {
            $select->where($where);
        }
		//echo $select;exit;
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
	
	public function getQuotefileCount($where = "") {
        $select = $this->select()
                ->from(array("qf" => $this->QUOTES_FILE_TBL), Array('cnt' => 'count(qf_id)'))
			    ->where("qf_status != 2");
        if ($where != '') {
            $select->where($where);
        }

        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }
	
	public function deleteQouteFile($qfArray) {
        if (is_array($qfArray)) {
            foreach ($qfArray as $_apt) {
                $aptData = Array();
                $aptData = $this->fetchENTRYBYImage($_apt);
                if (count($aptData) > 0) {
                    
                    $data = array("qf_status" => "2");
                    $this->update($data, "qf_id =" . (int) $aptData['qf_id']);
                }
            }
        } else {
            $build_id = $qfArray;
            $aptData = $this->fetchENTRYBYImage($build_id);
            if (count($aptData) > 0) {
                $data = array("qf_status" => "2");
                $this->update($data, "qf_id =" . $aptData['qf_id']);
            }
        }
    }
}